package mock.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import mock.bo.CashTransfer;
import mock.bo.CashTransferImp;
import mock.bo.CheckBalance;
import mock.bo.CheckBalanceImp;
import mock.bo.Logging;
import mock.bo.LoggingImp;
import mock.dao.AccountDAO;
import mock.dao.AccountDAOImp;
import mock.dao.CustomerDAO;
import mock.dao.CustomerDAOImp;
import mock.dto.AccountDTO;
import mock.dto.CustomerDTO;

import com.opensymphony.xwork2.ActionSupport;

public class CashTransferAction extends ActionSupport implements SessionAware{
	
	private CashTransfer ct = new CashTransferImp();
	private CheckBalance cb = new CheckBalanceImp();
	private Logging logBO = new LoggingImp();
	
	private String txtReceiveID;
	private String txtAmount;
	private String transferName;
	private String receiverName;
	private Map<String, Object> session;
	
	
	
	public String getReceiverName() {
		return receiverName;
	}

	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	public String getTransferName() {
		return transferName;
	}

	public void setTransferName(String transferName) {
		this.transferName = transferName;
	}

	public String getTxtAmount() {
		return txtAmount;
	}

	public void setTxtAmount(String txtAmount) {
		this.txtAmount = txtAmount;
	}

	public String getTxtReceiveID() {
		return txtReceiveID;
	}

	public void setTxtReceiveID(String txtReceiveID) {
		this.txtReceiveID = txtReceiveID;
	}
	
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	/**
	 * lay thong tin cua nguoi gui tien
	 * @return
	 */
	public String transferInfo(){
		String cardNo = (String) session.get("cardNo");
		AccountDTO account = ct.getAccountInformation(cardNo);
		CustomerDTO customer = ct.getCustomerInformation(cardNo);
		session.put("transferAccountID", account.getAccountID());
		setTransferName(customer.getName());
		return "transferInfoSuccess";
	}
	
	/**
	 * lay thong tin cua nguoi nhan tien
	 * @return
	 */
	public String receiverInfo(){
		//lay accountID va customer name cua nguoi nhan tien
		int temp;
		try{
			temp = Integer.parseInt(txtReceiveID);
		}catch(NumberFormatException e){
			return "receiverInfoFail";
		}
		int transferAccountID = (int) session.get("transferAccountID");
		if(temp == transferAccountID) return "receiverInfoFail";
		CustomerDTO receiverCustomer = ct.getReceiverCustomerInformation(temp);
		if(receiverCustomer != null){
			session.put("receiverAccountID", temp);
			session.put("receiverName", receiverCustomer.getName());
			setReceiverName(receiverCustomer.getName());
			return "receiverInfoSuccess";
		}
		return "receiverInfoFail";
	}

	/**
	 * kiem so so tien nguoi dung nhap
	 * @return
	 */
	public String checkAmount(){
		String cardNo = (String) session.get("cardNo");
		//kiem tra amount nguoi dung nhap co parse duoc qua double khong
		double amount;
		try{
			amount = Double.parseDouble(txtAmount);
		}catch(NumberFormatException e){
			return "checkAmountFail";
		}
		//kiem tra amount nguoi dung nhap co lon hon balance khong
		double balance = cb.checkBalance(cardNo);
		if(amount > balance) return "checkAmountFail";
		//kiem tra amount <= 0
		if(amount <= 0) return "checkAmountFail";
		session.put("amount", amount);
		return "checkAmountSuccess";
	}

	/**
	 * thuc hien chuyen tien
	 */
	public String process(){
		String cardNo = (String) session.get("cardNo");
		int atmID = (int) session.get("ATMID");
		//goi ham cua BO thuc hien chuyen tien
		int transferAccountID = (int)session.get("transferAccountID");
		int receiverAccountID = (int)session.get("receiverAccountID");
		double amount = (double)session.get("amount");
		session.remove("transferAccountID");
		session.remove("receiverAccountID");
		session.remove("amount");
		session.remove("receiverName");
		boolean chk = ct.processCashTransfer(transferAccountID, receiverAccountID, amount);
		if(chk == true){
			//ghi log
			logBO.logCashTransfer(atmID, cardNo, amount, receiverAccountID);
			return "processSuccess";}
		return "processFail";
	}

}
